home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS26.ADF / HexDump / HexDump.mod < prev    next >
Text File  |  1989-01-26  |  7KB  |  223 lines

  1. MODULE HexMemoryDump;
  2.  
  3. (* This program dumps the hex listing of memory. *)
  4.  
  5. FROM InOut         IMPORT WriteString, WriteLn, WriteCard,
  6.                           Read, EOL;
  7. FROM LongInOut     IMPORT WriteLongCard, WriteLongHex;
  8.  
  9. FROM Strings       IMPORT Length;
  10.  
  11. FROM SYSTEM        IMPORT BYTE, LONGWORD, ADDRESS, TSIZE;
  12.  
  13. TYPE
  14.    LocationType = ARRAY[0..79] OF CHAR;
  15.    CharType     = ARRAY[1..16] OF CHAR;
  16. VAR
  17.    StartingLocation : LocationType;
  18.    EndingLocation   : LocationType;
  19.    Start            : LONGCARD;
  20.    End              : LONGCARD;
  21.    Delta            : LONGCARD;
  22.    StartDump        : LONGWORD;
  23.    StartDumpPtr     : ADDRESS;
  24.    LeftByte         : CARDINAL;
  25.    RightByte        : CARDINAL;
  26.    CharOut          : CharType;
  27.    CharIndex        : CARDINAL;
  28.  
  29.    PROCEDURE ReadInput(VAR Input : LocationType); (* Input/Output *)
  30.  
  31.    (* This procedure reads an input string typed in from the 
  32.       keyboard. *)
  33.  
  34.    VAR
  35.       x  : CARDINAL;
  36.       ch : CHAR;
  37.    BEGIN
  38.       x := 0;
  39.       LOOP
  40.       (* Read the character and captialize. *)
  41.          Read(ch);
  42.          Input[x] := CAP(ch);
  43.          (* If EOL or max length of 80 then exit the loop. *)
  44.          IF ((ch = EOL) OR (x = 79)) THEN
  45.             EXIT;
  46.          ELSE
  47.             x := x + 1;
  48.          END; (* IF *)
  49.       END; (* LOOP *)
  50.    END ReadInput;
  51.  
  52.    PROCEDURE ConvertHexStringToCardinal(VAR Num     : LONGCARD;
  53.                                         NumLocation : LocationType);
  54.  
  55.    (* This procedure converts the hex input string to cardinal in
  56.       order to convert to an address. *)
  57.  
  58.    VAR
  59.       Index    : INTEGER;
  60.       Temp     : CARDINAL;
  61.       Temp1    : LONGCARD;
  62.       Dummy    : CHAR;
  63.       MaxCount : INTEGER;
  64.    BEGIN
  65.       Index := 0;
  66.       MaxCount := Length(NumLocation) - 1;
  67.       WHILE Index < MaxCount DO
  68.          Dummy := NumLocation[Index];
  69.          IF((Dummy >= 'A') AND (Dummy <= 'F')) THEN
  70.             Temp := ORD(Dummy) - ORD('A') + 10;
  71.          ELSE
  72.             Temp := ORD(Dummy) - ORD('0');
  73.          END;(* IF *)
  74.          Temp1 := LONGCARD(Temp);
  75.          Num := 16 * Num + Temp1;
  76.          Index := Index + 1;
  77.       END; (* WHILE *)
  78.    END ConvertHexStringToCardinal;
  79.  
  80.    PROCEDURE HexDump( StartAddress : ADDRESS;
  81.                       WordCount    : LONGCARD);
  82.  
  83.       (* This procedure dumps memory in hex. *)
  84.  
  85.       VAR
  86.          loc   : ADDRESS;
  87.          count : LONGCARD;
  88.  
  89.       BEGIN
  90.          loc := StartAddress;
  91.          FOR count := 1 TO WordCount DO
  92.          (* Write the line starting address and keep line to a length
  93.             of eight.*)
  94.             IF count MOD 8 = 1 THEN
  95.                WriteString(CharOut);
  96.                WriteLn;
  97.                WriteLongHex(LONGCARD(loc),8);
  98.                WriteString(': ');
  99.             END;(* IF *)
  100.             (* Write the hex value of memory. *)
  101.             WriteHex(CARDINAL(loc^),LeftByte,RightByte);
  102.             INC(loc,TSIZE(CARDINAL))
  103.          END;(* FOR *)
  104.          WriteLn;
  105.          RETURN;
  106.    END HexDump;
  107.  
  108.    PROCEDURE WriteHex(    Input     : CARDINAL;
  109.                       VAR LeftByte  : CARDINAL;
  110.                       VAR RightByte : CARDINAL);
  111.  
  112. (* This procedure writes out the memory contents in hex. *)
  113.  
  114.    TYPE
  115.       OutType     = ARRAY[0..3] OF CHAR;
  116.       DivisorType = ARRAY[0..3] OF CARDINAL;
  117.    VAR
  118.       Out     : OutType;
  119.       Index   : CARDINAL;
  120.       Temp    : CARDINAL;
  121.       Divisor : DivisorType;
  122.       x       : CARDINAL;
  123.  
  124.    PROCEDURE HexOut(Temp  : CARDINAL;
  125.                     VAR Out : OutType;
  126.                     Index : CARDINAL);
  127.  
  128.    (* This procedure converts memory contents to hex. *)
  129.       BEGIN
  130.          CASE Temp OF
  131.             0 : Out[Index] := '0' |
  132.             1 : Out[Index] := '1' |
  133.             2 : Out[Index] := '2' |
  134.             3 : Out[Index] := '3' |
  135.             4 : Out[Index] := '4' |
  136.             5 : Out[Index] := '5' |
  137.             6 : Out[Index] := '6' |
  138.             7 : Out[Index] := '7' |
  139.             8 : Out[Index] := '8' |
  140.             9 : Out[Index] := '9' |
  141.            10 : Out[Index] := 'A' |
  142.            11 : Out[Index] := 'B' |
  143.            12 : Out[Index] := 'C' |
  144.            13 : Out[Index] := 'D' |
  145.            14 : Out[Index] := 'E' |
  146.            15 : Out[Index] := 'F';
  147.          END; (* CASE *)
  148.          RETURN;
  149.    END HexOut;
  150.  
  151.    BEGIN
  152.       Divisor[0] := 4096;
  153.       Divisor[1] := 256;
  154.       Divisor[2] := 16;
  155.       Divisor[3] := 1;
  156.       FOR Index := 0 TO 3 DO
  157.          IF Index = 2 THEN
  158.             RightByte := Input;
  159.             LeftByte  := 0;
  160.             FOR x := 0 TO 1 DO
  161.                IF((Out[x] >= 'A') AND (Out[x] <= 'F')) THEN
  162.                   LeftByte := 16 * LeftByte + (ORD(Out[x]) -
  163.                              ORD('A') + 10);
  164.                ELSE
  165.                   LeftByte := 16 * LeftByte + ORD(Out[x]) - ORD('0');
  166.                END; (* IF *)
  167.             END; (* FOR *)
  168.          END; (* IF *)
  169.          Temp := Input DIV Divisor[Index];
  170.          HexOut(Temp,Out,Index);
  171.          Input := Input - Temp * Divisor[Index];
  172.       END; (* FOR *)
  173.       WriteChar(CharOut,CharIndex,LeftByte,RightByte);
  174.       WriteString(Out);
  175.       WriteString(' ');
  176.    END WriteHex;
  177.  
  178.    PROCEDURE WriteChar(VAR CharOut   : CharType;
  179.                        VAR CharIndex : CARDINAL;
  180.                            LeftByte  : CARDINAL;
  181.                            RightByte : CARDINAL);
  182.  
  183.    (* This procedure writes out the ASCII equivalent of the memory
  184.       contents. *)
  185.  
  186.    BEGIN
  187.       IF((CHR(19H)<CHR(LeftByte))AND(CHR(LeftByte)<CHR(7EH))) THEN
  188.          CharOut[CharIndex] := CHR(LeftByte);
  189.       ELSE
  190.          CharOut[CharIndex] := CHR(2EH);
  191.       END; (* IF *)
  192.       CharIndex := CharIndex + 1;
  193.       IF((CHR(19H)<CHR(RightByte))AND(CHR(RightByte)<CHR(7EH))) THEN
  194.          CharOut[CharIndex] := CHR(RightByte);
  195.       ELSE
  196.          CharOut[CharIndex] := CHR(2EH);
  197.       END; (* IF *)
  198.       CharIndex := CharIndex + 1;
  199.       IF CharIndex = 17 THEN CharIndex := 1 END;
  200.    END WriteChar;
  201.  
  202. BEGIN
  203. (* Initialize parameters. *)
  204.    Start := 0;
  205.    CharIndex := 1;
  206. (* Get starting memory location. *)
  207.    WriteString('Enter starting memory location: ');
  208.    ReadInput(StartingLocation);
  209. (* Convert to value. *)
  210.    ConvertHexStringToCardinal(Start,StartingLocation);
  211. (* Get ending memory location. *)
  212.    WriteString('Enter ending memory location: ');
  213.    ReadInput(EndingLocation);
  214. (* Convert to value. *)
  215.    ConvertHexStringToCardinal(End,EndingLocation);
  216. (* Calculate number of bytes to output. *)
  217.    Delta := (End - Start) DIV 2;
  218. (* Calculate starting address. This is important. *)
  219.    StartDump := LONGWORD(Start);
  220.    StartDumpPtr := ADDRESS(StartDump);
  221. (* Dump memory. *)
  222.    HexDump(StartDumpPtr,Delta);
  223. END HexMemoryDump.